﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TrainTicketBookingSystem
{
    public partial class RegisterForm : Form
    {
        UserService userService = new UserService();
        public RegisterForm()
        {
            InitializeComponent();
        }

        private void RegisterForm_Load(object sender, EventArgs e)
        {

        }

        private void RegisterForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            LoginFrom.loginFrom.Show();
        }

        private void btnModifyRegister_Click(object sender, EventArgs e)
        {
            int id = userService.findMaxId() + 1;
            string account = textBoxAccount.Text.Trim();
            string password = textBoxPwd.Text.Trim();
            string name = textBoxUserName.Text.Trim();
            string sex = comboBoxSex.Text.Trim();
            string phonenum = textBoxPhoneNum.Text.Trim();
            string certificateNum = textBoxCertificate_num.Text.Trim();

            if (account == "" || password == "" ||
                name == "" || sex == "" ||
                phonenum == "" || certificateNum == "")
            {
                MessageBox.Show("请将数据补充完好");
                return;
            }

            if(sex != "男" && sex != "女" && sex != "未知")
            {
                sex = "未知";
            }

            int rs = regist(new User(id, account, password, name, sex, phonenum, certificateNum));

            MessageBox.Show(rs == 0 ? "账号重复，请再重新设计一个账号":
                rs == -1 ? "该身份证号已注册账号" : "注册成功，请返回登陆");
        }

        //已有相同账号 返回0，已有相同身份证号 返回-1，成功返回 1
        private int regist(User u)
        {
            return userService.Regist(u);
        }

        private void brnReturnLogin_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
